package com.yylending.las.lending.process;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.Constants;
import com.yylending.las.common.widgets.CommonDialog;
import com.yylending.las.common.widgets.TelephoneTextView;
import com.yylending.las.lending.QuotaEvent;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 申请额度
 */
public class IncreaseSuccessActivity extends BaseActivity implements CommonDialog.ButtonClickListener {
	@BindView(R.id.telephone)
	TelephoneTextView mTelephone;

	/**
	 * 拨打客服电话Dialog
	 */
	private CommonDialog mCallDialog;
	/**
	 * 客服电话
	 */
	private String mPhoneNumber;

	public static void start(Activity activity) {
		Intent intent = new Intent(activity, IncreaseSuccessActivity.class);
		activity.startActivity(intent);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_increase_success);
		EventAgent.register(this);
		ButterKnife.bind(this);
		setTitle(R.string.increase_quota_apply_title);
		mTelephone.setPageNo(Constants.PAGE_NO_INCREASE_SUCCESS);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Global.setThirdPartyPageOpenedFlag(false);
	}

	@Override
	protected void onDestroy() {
		dismissCallDialog();
		mCallDialog = null;
		EventAgent.unregister(this);
		super.onDestroy();
	}

	@OnClick({R.id.next_step_btn})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.next_step_btn:
				finish();
				break;
		}
	}

	@Override
	public void onCancel(CommonDialog dialog) {
		dismissCallDialog();
	}

	@Override
	public void onConfirm(CommonDialog dialog) {
		dismissCallDialog();
		//忽略手势密码
		Global.setThirdPartyPageOpenedFlag(true);
		YJKD.tel(this, mPhoneNumber.replaceAll("-", ""));
	}

	private void showCallDialog() {
		dismissCallDialog();
		mCallDialog = new CommonDialog(this, null, mPhoneNumber, getString(R.string.call), getString(R.string.cancel), this, true);
		mCallDialog.show();
	}

	private void dismissCallDialog() {
		if (mCallDialog != null && mCallDialog.isShowing()) {
			mCallDialog.dismiss();
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onMessageEvent(QuotaEvent.ShowCallDialogEvent event) {
		switch (event.id) {
			case QuotaEvent.Event.QUOTA_SHOW_CALL_DIALOG:
				if (Constants.PAGE_NO_INCREASE_SUCCESS == event.page) {
					mPhoneNumber = event.phoneNumber;
					showCallDialog();
				}
				break;
		}
	}
}
